//
//  UserPartHeaderView.m
//  SmallElephant
//
//  Created by Faith on 2024/11/10.
//

#import "UserPartHeaderView.h"
#import "../Login/LoginController.h"

@interface UserPartHeaderView()

/// 头像视图
@property (nonatomic, strong) UIImageView *avatarView;

/// 用户信息视图
@property (nonatomic, strong) UIView *infoView;

/// 昵称视图
@property (nonatomic, strong) UILabel *nicknameView;

/// ID视图
@property (nonatomic, strong) UILabel *idView;

/// 二维码视图
@property (nonatomic, strong) UIImageView *btnQrcode;

/// 更多按钮视图
@property (nonatomic, strong) UIButton *btnMore;

@end



@implementation UserPartHeaderView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupViews];
    }
    return self;
}

/// 设置视图
- (void)setupViews {
    [self applyStyle:@"display: flex; flex-direction: row;"];
    // 添加头像视图
    [self addSubview:self.avatarView style:@"display: flex; width: 50px; height: 50px; border-radius: 25px; margin-left: 15px;"];
    // 添加信息视图
    [self addSubview:self.infoView style:@"display: flex; flex:1; justify-content: center; margin-left: 12px;"];
    // 二维码视图
    [self addSubview:self.btnQrcode style:@"display: flex; width: 28px; height: 28px; align-self: center; margin-left: 10px;"];
    // 更多按钮视图
    [self addSubview:self.btnMore style:@"display: flex; width: 24px; height: 24px; align-self: center; margin-left: 10px; margin-right: 10px;"];
    
}

/// 懒加载头像视图
- (UIImageView *)avatarView {
    if(!_avatarView) {
        _avatarView = [[UIImageView alloc] init];
        _avatarView.userInteractionEnabled = YES;
        [_avatarView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnAvatarTapEventHandler)]];
    }
    return _avatarView;
}

/// 懒加载用户信息视图
- (UIView *)infoView {
    if(!_infoView) {
        _infoView = [[UIView alloc] init];
        _nicknameView = [[UILabel alloc] init];
        _nicknameView.numberOfLines = 0;
        [_infoView addSubview:_nicknameView style:@"display: flex; font-size: 16px; color: #333333;"];
        
        _idView = [[UILabel alloc] init];
        [_infoView addSubview:_idView style:@"display: flex; margin-top: 5px; font-size: 14px; color: #666666; "];
    }
    return _infoView;
}

/// 二维码按钮
- (UIImageView *)btnQrcode {
    if(!_btnQrcode) {
        _btnQrcode = [[UIImageView alloc] init];
        [_btnQrcode setImage:[UIImage systemImageNamed:@"qrcode"]];
        _btnQrcode.tintColor = [UIColor colorWithHexString:@"#000000"];
    }
    return _btnQrcode;
}

/// 更多按钮
- (UIButton *)btnMore {
    if(!_btnMore) {
        _btnMore = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnMore setImage:[UIImage systemImageNamed:@"chevron.right"] forState:UIControlStateNormal];
        _btnMore.tintColor = [UIColor colorWithHexString:@"#939393"];
    }
    return _btnMore;
}

- (void)btnAvatarTapEventHandler {
    UIViewController *loginController = [[UIViewController alloc] init];
    [[self getRootController] pushViewController:loginController checkPermission:YES];
}


/// 设置数据
- (void)setData:(NSDictionary*)data {
    [self.avatarView setImage:[UIImage imageWithName:data[@"avatar"]]];
    [self.avatarView applyStyle];
    self.nicknameView.text = data[@"nickname"];
    [self.nicknameView applyStyle];
    self.idView.text = data[@"id"];
    [self.idView applyStyle];
}

- (void)layoutSubviews {
    [self.superview.yoga applyLayoutPreservingOrigin:YES];
}

@end
